/**
 *
 * Copyright (c) 2012 NCS Corporation.
 *
 * Author: tangb
 * Created Date: May 30, 2012
 * 
 * 	version					author					remark
 * 	1.0						tangb				
 */
package com.ocgs.base.util;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

public class PropertyUtil {

	private static Map<String, Object> table = new WeakHashMap<String, Object>();
	private static long reloadPeriod = 5000;
	private static final Log log = LogFactory.getLog(PropertyUtil.class);

	public PropertyUtil() {
	}

	/**
	 * @throws Exception
	 * 
	 * @Description get property by key with no exception
	 * @param propertyFileName
	 * @param key
	 * @throws Exception
	 * @return String
	 * @throws
	 */
	public static String getPropertyValueNoEx(String propertyFileName, String key) {
		try {
			return getPropertyValue(propertyFileName, key);
		} catch (Exception exception) {
			log.error("getPropertyValueNoEx", exception);
		}
		return "";
	}

	/**
	 * @throws Exception
	 * 
	 * @Description get property by key
	 * @param propertyFileName
	 * @param key
	 * @throws Exception
	 * @return String
	 * @throws
	 */
	public static String getPropertyValue(String propertyFileName, String key) throws Exception {
		String result = null;

		PropertiesConfiguration config = getConfig(propertyFileName);
		Object returnObj = config.getProperty(key);
		if (returnObj != null) {
			if (String.class.getName().equals(returnObj.getClass().getName())) {
				result = (String) returnObj;
			} else if (ArrayList.class.getName().equals(returnObj.getClass().getName())) {
				returnObj = ((ArrayList<?>) returnObj).get(0);
				if (String.class.getName().equals(returnObj.getClass().getName())) {
					result = (String) returnObj;
				}
			}
		}

		return result;
	}

	/**
	 * @throws Exception
	 * 
	 * @Description get property by key
	 * @param propertyFileName
	 * @param key
	 * @throws Exception
	 * @return String
	 * @throws
	 */
	public static String getList2String(String propertyFileName, String key) throws Exception {
		String result = null;

		PropertiesConfiguration config = getConfig(propertyFileName);
		Object returnObj = config.getProperty(key);
		if (returnObj != null) {
			if (String.class.getName().equals(returnObj.getClass().getName())) {
				result = (String) returnObj;
			} else if (ArrayList.class.getName().equals(returnObj.getClass().getName())) {
				@SuppressWarnings("unchecked")
				List<String> pList = (List<String>) returnObj;
				StringBuffer pbuff = new StringBuffer();
				for (int i = 0, j = pList.size(); i < j; i++) {
					pbuff.append(pList.get(i));
					if (i != j - 1)
						pbuff.append(",");
				}
				result = pbuff.toString();
			}
		}

		return result;
	}

	/**
	 * 
	 * @Description get properties by key,return a array(single key corresponding multiple value)
	 * @param propertyFileName
	 * @param key
	 * @throws Exception
	 * @return String[]
	 * @throws
	 */
	public static String[] getPropertyValueArray(String propertyFileName, String key) throws Exception {
		if (StringUtils.isBlank(key))
			throw new Exception("Property key is blank!");

		String[] result = null;
		PropertiesConfiguration config = getConfig(propertyFileName);
		result = (String[]) config.getStringArray(key);

		return result;
	}

	/**
	 * 
	 * @Description get configuration of the specific file
	 * @param propertyFileName
	 * @throws Exception
	 * @return PropertiesConfiguration
	 * @throws
	 */
	private static PropertiesConfiguration getConfig(String propertyFileName) throws Exception {
		if (StringUtils.isBlank(propertyFileName))
			throw new Exception("propertyFileName is blank ");

		PropertiesConfiguration config = (PropertiesConfiguration) table.get(propertyFileName);

		if (config == null) {
			config = new PropertiesConfiguration(propertyFileName);
			FileChangedReloadingStrategy fs = new FileChangedReloadingStrategy();
			fs.setRefreshDelay(reloadPeriod);
			config.setReloadingStrategy(fs);
			table.put(propertyFileName, config);
		}
		return config;

	}

}
